import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../viewmodels/music_player_view_model.dart';
import '../../models/music_item_model.dart';

/// 歌曲信息展示组件
/// 显示歌曲标题、艺术家、专辑等信息
class SongInfoWidget extends StatelessWidget {
  /// 歌曲信息（如果不提供，则从 ViewModel 获取当前播放歌曲）
  final MusicItemModel? music;

  /// 标题样式
  final TextStyle? titleStyle;

  /// 艺术家样式
  final TextStyle? artistStyle;

  /// 专辑样式
  final TextStyle? albumStyle;

  /// 文本对齐方式
  final TextAlign textAlign;

  /// 是否显示专辑信息
  final bool showAlbum;

  /// 最大行数（0 表示不限制）
  final int maxLines;

  /// 文本溢出处理
  final TextOverflow overflow;

  const SongInfoWidget({
    Key? key,
    this.music,
    this.titleStyle,
    this.artistStyle,
    this.albumStyle,
    this.textAlign = TextAlign.center,
    this.showAlbum = true,
    this.maxLines = 1,
    this.overflow = TextOverflow.ellipsis,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Consumer<MusicPlayerViewModel>(
      builder: (context, viewModel, child) {
        final currentMusic = music ?? viewModel.currentTrack;

        if (currentMusic == null) {
          return _buildEmptyState(context);
        }

        return Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            // 歌曲标题
            Text(
              currentMusic.title,
              style: titleStyle ?? Theme.of(context).textTheme.titleLarge,
              textAlign: textAlign,
              maxLines: maxLines > 0 ? maxLines : null,
              overflow: overflow,
            ),

            const SizedBox(height: 4),

            // 艺术家
            Text(
              currentMusic.artist,
              style: artistStyle ??
                  Theme.of(context).textTheme.bodyMedium?.copyWith(
                        color: Theme.of(context)
                            .textTheme
                            .bodyMedium
                            ?.color
                            ?.withOpacity(0.7),
                      ),
              textAlign: textAlign,
              maxLines: maxLines > 0 ? maxLines : null,
              overflow: overflow,
            ),

            // 专辑信息
            if (showAlbum && currentMusic.album.isNotEmpty) ...[
              const SizedBox(height: 2),
              Text(
                currentMusic.album,
                style: albumStyle ??
                    Theme.of(context).textTheme.bodySmall?.copyWith(
                          color: Theme.of(context)
                              .textTheme
                              .bodySmall
                              ?.color
                              ?.withOpacity(0.6),
                        ),
                textAlign: textAlign,
                maxLines: maxLines > 0 ? maxLines : null,
                overflow: overflow,
              ),
            ],
          ],
        );
      },
    );
  }

  /// 构建空状态显示
  Widget _buildEmptyState(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Text(
          '暂无播放内容',
          style: titleStyle ??
              Theme.of(context).textTheme.titleLarge?.copyWith(
                    color: Theme.of(context)
                        .textTheme
                        .titleLarge
                        ?.color
                        ?.withOpacity(0.5),
                  ),
          textAlign: textAlign,
        ),
        const SizedBox(height: 4),
        Text(
          '请选择音乐开始播放',
          style: artistStyle ??
              Theme.of(context).textTheme.bodyMedium?.copyWith(
                    color: Theme.of(context)
                        .textTheme
                        .bodyMedium
                        ?.color
                        ?.withOpacity(0.5),
                  ),
          textAlign: textAlign,
        ),
      ],
    );
  }
}

/// 紧凑版歌曲信息组件
/// 适用于悬浮播放器等空间有限的场景
class CompactSongInfoWidget extends StatelessWidget {
  /// 歌曲信息
  final MusicItemModel? music;

  /// 文本样式
  final TextStyle? textStyle;

  /// 分隔符
  final String separator;

  const CompactSongInfoWidget({
    Key? key,
    this.music,
    this.textStyle,
    this.separator = ' - ',
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Consumer<MusicPlayerViewModel>(
      builder: (context, viewModel, child) {
        final currentMusic = music ?? viewModel.currentTrack;

        if (currentMusic == null) {
          return Text(
            '暂无播放内容',
            style: textStyle ??
                Theme.of(context).textTheme.bodyMedium?.copyWith(
                      color: Theme.of(context)
                          .textTheme
                          .bodyMedium
                          ?.color
                          ?.withOpacity(0.5),
                    ),
            overflow: TextOverflow.ellipsis,
          );
        }

        final displayText =
            '${currentMusic.title}$separator${currentMusic.artist}';

        return Text(
          displayText,
          style: textStyle ?? Theme.of(context).textTheme.bodyMedium,
          overflow: TextOverflow.ellipsis,
          maxLines: 1,
        );
      },
    );
  }
}
